<?php 
namespace App\Http\Controllers\OAuth;

use App\Ext\Curl;
use App\Model\User;
use App\Http\Controllers\Controller;
use Session;

class GithubCallback extends Controller
{

	private $ClientID;

	private $ClientSecret;

	public function __construct()
    	{
    		$this->middleware('OAuth20');
		$this->ClientID = config('OAuth.Github.ClientID');
		$this->ClientSecret = config('OAuth.Github.ClientSecret');

	}

	public function Index()
	{
		$url = 'https://github.com/login/oauth/access_token?client_id='.$this->ClientID.'&client_secret='.$this->ClientSecret.'&code='.$_GET['code'];
		$token = Curl::New_Ch()->Default_Get_Set()->Https()->Exec($url);
		parse_str(urldecode($token), $output);
		$url = 'https://api.github.com/user';
		$header[] = 'Authorization: token '.$output['access_token'];
		$header[] = 'User-Agent: Awesome-Octocat-App';
		$Info = Curl::New_Ch()->Default_Get_Set()->Header_Set($header)->Https()->Exec($url);
		$Info = json_decode($Info);
		$User_Info['name'] = $Info->login;
		$User_Info['oauthid'] = $Info->id;
		$User_Info['id'] = User::Add_User($User_Info);
		session($User_Info);
		return redirect('/');
		// var_dump($User_Info);
	}

}